home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEX-UTIL / DVIPS_55 / dvips / src / c / repack < prev    next >
Text File  |  1994-05-06  |  14KB  |  457 lines

  1. /*
  2.  *   Compressed TeX fonts in PostScript.
  3.  *   By Radical Eye Software.
  4.  *   (Slight mods by Don Knuth in December 89.)
  5.  */
  6. #include "dvips.h" /* The copyright notice in that file is included too! */
  7. #ifdef DEBUG
  8. extern integer debug_flag;
  9. #endif /* DEBUG */
  10.  
  11. /*   Given a raster that has been unpacked from PK format,
  12.  *   we compress it by another scheme that is suitable for
  13.  *   a PostScript-oriented unpacking. We write instructions for
  14.  *   a little interpreter whose one-byte instructions have the form
  15.  *   18*opcode+parameter. The interpreter forms each row based
  16.  *   on simple transformations to the previous row. */
  17.  
  18. #define MAXOUT (18)
  19. #define CMD(n) (MAXOUT*(n))
  20. #define ADVXCHG1 CMD(0)
  21. #define ADVXCHG1END CMD(1)
  22. #define CHGX CMD(2)-1
  23. #define CHGXEND CMD(3)-1
  24. #define ADVXLSH CMD(4)
  25. #define ADVXLSHEND CMD(5)
  26. #define ADVXRSH CMD(6)
  27. #define ADVXRSHEND CMD(7)
  28. #define ADVX CMD(8)-1
  29. #define REPX CMD(9)-1
  30. #define SETX CMD(10)-1
  31. #define CLRX CMD(11)-1
  32. #define ADVXCHG2 CMD(12)
  33. #define ADVXCHG2END CMD(13)
  34. #define END CMD(14)
  35.  
  36. extern void error() ;
  37. extern long getlong() ;
  38. extern long unpack() ;
  39.  
  40. static int rowlength = -1 ;
  41. static unsigned char *specdata ;
  42. static long tslen = 0 ;
  43. static unsigned char *tempstore, *tsp, *tsend ;
  44.  
  45. void putlong(a, i)
  46. register char *a ;
  47. long i ;
  48. {
  49.    a[0] = i >> 24 ;
  50.    a[1] = i >> 16 ;
  51.    a[2] = i >> 8 ;
  52.    a[3] = i ;
  53. }
  54.  
  55. long getlong(a)
  56. register unsigned char *a ;
  57. {
  58.    return ((((((a[0] << 8L) + a[1]) << 8L) + a[2]) << 8L) + a[3]) ;
  59. }
  60.  
  61. /* First, a routine that appends one byte to the compressed output. */
  62.  
  63. #define addtse(n) {lcm=tsp-tempstore;addts(n);} /* mark END option position */
  64.  
  65. static void addts(what)
  66. register unsigned char what ;
  67. {
  68.    register unsigned char *p, *q ;
  69.  
  70.    if (tsp >= tsend) {
  71.       if (tempstore == NULL) {
  72.          tslen = 2020 ;
  73.          tempstore = (unsigned char *)mymalloc((integer)tslen) ;
  74.          tsp = tempstore ;
  75.       } else {
  76.          tslen = 2 * tslen ;
  77.          tsp = (unsigned char *)mymalloc((integer)tslen) ;
  78.          for (p=tempstore, q=tsp; p<tsend; p++, q++)
  79.             *q = *p ;
  80.          free((char *)tempstore) ;
  81.          tempstore = tsp ;
  82.          tsp = q ;
  83.       }
  84.       tsend = tempstore + tslen ;
  85.    }
  86.    *tsp++ = what ;
  87. }
  88.  
  89. /* Next, a routine that discovers how to do the compression. */
  90.  
  91. #define rsh(a,b) ( ((a)==0) ? ((b)==128) : ( ((a)==255) ? ((b)==127) :\
  92.                                     ((b)==(((a)>>1)|((a)&128))) ))
  93. #define lsh(a,b) ( ((a)==0) ? ((b)==1) : ( ((a)==255) ? ((b)==254) :\
  94.                                     ((b)==((((a)<<1)&255)|((a)&1))) ))
  95. #define DIFFERENT (1)
  96. #define LSHPOSSIB (2)
  97. #define RSHPOSSIB (4)
  98. #define BLKPOSSIB (8)
  99. #define WHTPOSSIB (16)
  100. #define ENDROW (32)
  101. #define NOPOSSIB(n) ((n&(LSHPOSSIB|RSHPOSSIB|BLKPOSSIB|WHTPOSSIB))==0)
  102. #define NOSHIFT(n) ((n&(LSHPOSSIB|RSHPOSSIB))==0)
  103. /*
  104.  *   Our input bytes are packed to the 16-bit word.  On output,
  105.  *   they're packed to bytes. Thus, we may have to skip a byte at
  106.  *   the end of each row.
  107.  */
  108. void dochar(from, width, height)
  109. unsigned char *from ;
  110. short width, height ; /* in bytes */
  111. {
  112.    register int i ;
  113.    register unsigned char *f, *t, *d ;
  114.    register unsigned char *e ;
  115.    register int accum ;
  116.    int j, k, kk ;
  117.    int diffrow ;
  118.    int repeatcount ;
  119.    int lit, pos, cmd = 0 ;
  120.    long lcm ;
  121.    int widthc ;
  122.  
  123.    widthc = width + (width & 1) ; /* halfword correction */
  124.    lcm = -1 ;
  125.    if (widthc > rowlength) {
  126.       if (rowlength > 0)
  127.          free((char *)specdata) ;
  128.       rowlength = widthc + 30 ;
  129.       specdata = (unsigned char *)mymalloc((integer)(rowlength + 15)) ;
  130.    }
  131.    for (i= -15, t=specdata; i<=widthc; i++, t++)
  132.       *t = 0 ;
  133.    repeatcount = 0 ;
  134.    f = specdata + 2 ;
  135.    for (j=0; j<height; j++, f = from, from += widthc) {
  136.       diffrow = 0 ;
  137.       for (i=0, t=from, d=specdata; i<width; i++, f++, t++, d++) {
  138.          if (*f == *t) {
  139.             if (*t == 0)
  140.                *d = WHTPOSSIB ;
  141.             else if (*t == 255)
  142.                *d = BLKPOSSIB ;
  143.             else
  144.                *d = 0 ;
  145.          } else {
  146.             accum = DIFFERENT ;
  147.             if (rsh(*f, *t))
  148.                accum |= RSHPOSSIB ;
  149.             else if (lsh(*f, *t))
  150.                accum |= LSHPOSSIB ;
  151.             if (*t == 0)
  152.                accum |= WHTPOSSIB ;
  153.             else if (*t == 255)
  154.                accum |= BLKPOSSIB ;
  155.             *d = accum ;
  156.             diffrow++ ;
  157.          }
  158.       } /* end 'for i' */
  159.       *d = ENDROW ;
  160.       if (diffrow == 0) {
  161.          repeatcount++ ;
  162.       } else {
  163.          if (repeatcount) {
  164.             while (repeatcount > MAXOUT) {
  165.                addts((unsigned char)(REPX+MAXOUT)) ;
  166.                repeatcount -= MAXOUT ;
  167.             }
  168.             addts((unsigned char)(REPX+repeatcount)) ;
  169.             repeatcount = 0 ;
  170.          }
  171.          pos = 0 ;
  172.          for (i=0, d=specdata, f=t-width; i<width;) {
  173.             if ((*d & DIFFERENT) == 0) {
  174.                i++ ;
  175.                d++ ;
  176.                f++ ;
  177.             } else {
  178.                accum = 0 ;
  179.                if (pos != i)
  180.                   lit = NOSHIFT(*d) ;
  181.                else /* N.B.: 'lit' does not imply literate programming here */
  182.                   lit = NOPOSSIB(*d) ;
  183.                for (e=d; ;e++) {
  184.                   if (NOPOSSIB(*e))
  185.                      lit = 1 ;
  186.                   if ((*e & DIFFERENT) == 0)
  187.                      break ;
  188.                   if ((*e & WHTPOSSIB) &&
  189.                       (e[1] & WHTPOSSIB)) {
  190.                      while (*e & WHTPOSSIB) {
  191.                         e++ ;
  192.                         accum++ ;
  193.                      }
  194.                      cmd = CLRX ;
  195.                      e -= accum ;
  196.                      break ;
  197.                   } else if ((*e & BLKPOSSIB) &&
  198.                       (e[1] & BLKPOSSIB)) {
  199.                      while (*e & BLKPOSSIB) {
  200.                         e++ ;
  201.                         accum++ ;
  202.                      }
  203.                      cmd = SETX ;
  204.                      e -= accum ;
  205.                      break ;
  206.                   }
  207.                } /* end 'for e'; d pts to first bad byte, e to next good one */
  208.                while (i - pos > MAXOUT) {
  209.                   addts((unsigned char)(ADVX+MAXOUT)) ;
  210.                   pos += MAXOUT ;
  211.                }
  212.                if (0 != (k = (e - d))) {
  213.                   if (lit) {
  214.                      if (k > 2) {
  215.                         if (i > pos) {
  216.                            addts((unsigned char)(ADVX + i - pos)) ;
  217.                            pos = i ;
  218.                         }
  219.                         while (k > MAXOUT) {
  220.                            addts((unsigned char)(CHGX + MAXOUT)) ;
  221.                            for (kk=0; kk<MAXOUT; kk++)
  222.                               addts((unsigned char)(*f++)) ;
  223.                            d += MAXOUT ;
  224.                            pos += MAXOUT ;
  225.                            i += MAXOUT ;
  226.                            k -= MAXOUT ;
  227.                         }
  228.                         addtse((unsigned char)(CHGX + k)) ;
  229.                         pos += k ;
  230.                         for (; d<e; d++, i++, f++)
  231.                            addts((unsigned char)(*f)) ;
  232.                      } else {
  233.                         if (k == 1) {
  234.                            if (i == pos+MAXOUT) {
  235.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  236.                               pos = i ;
  237.                            }
  238.                            addtse((unsigned char)(ADVXCHG1 + i - pos)) ;
  239.                            addts((unsigned char)(*f)) ;
  240.                            i++ ;
  241.                            pos = i ;
  242.                            d++ ;
  243.                            f++ ;
  244.                         } else {
  245.                            if (i == pos+MAXOUT) {
  246.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  247.                               pos = i ;
  248.                            }
  249.                            addtse((unsigned char)(ADVXCHG2 + i - pos)) ;
  250.                            addts((unsigned char)(*f)) ;
  251.                            addts((unsigned char)(f[1])) ;
  252.                            i += 2 ;
  253.                            pos = i ;
  254.                            d += 2 ;
  255.                            f += 2 ;
  256.                         }
  257.                      }
  258.                   } else {
  259.                      while (e > d) {
  260.                         if (*d & LSHPOSSIB) {
  261.                            if (i == pos+MAXOUT) {
  262.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  263.                               pos = i ;
  264.                            }
  265.                            addtse((unsigned char)(ADVXLSH + i - pos)) ;
  266.                         } else if (*d & RSHPOSSIB) {
  267.                            if (i == pos+MAXOUT) {
  268.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  269.                               pos = i ;
  270.                            }
  271.                            addtse((unsigned char)(ADVXRSH + i - pos)) ;
  272.                         } else if (*d & WHTPOSSIB) { /* i==pos */
  273.                            addts((unsigned char)(CLRX + 1)) ; lcm = -1 ;
  274.                         } else if (*d & BLKPOSSIB) { /* i==pos */
  275.                            addts((unsigned char)(SETX + 1)) ; lcm = -1 ;
  276.                         } else
  277.                            error("! bug") ; /* why wasn't lit true? */
  278.                         d++ ;
  279.                         f++ ;
  280.                         i++ ;
  281.                         pos = i ;
  282.                      } /* end 'while e>d' */
  283.                   }
  284.                } /* end 'if e>d' */
  285.                if (accum > 0) {
  286.                   if (i > pos) {
  287.                      addts((unsigned char)(ADVX + i - pos)) ;
  288.                      pos = i ;
  289.                   }
  290.                   i += accum ;
  291.                   d += accum ;
  292.                   f += accum ;
  293.                   while (accum > MAXOUT) {
  294.                      addts((unsigned char)(cmd + MAXOUT)) ;
  295.                      accum -= MAXOUT ;
  296.                      pos += MAXOUT ;
  297.                   }
  298.                   lcm = -1 ;
  299.                   addts((unsigned char)(cmd + accum)) ;
  300.                   pos += accum ;
  301.                }
  302.             } /* end 'else DIFFERENT' */
  303.          } /* end 'for i' */
  304.          if (lcm != -1) {
  305.             tempstore[lcm] += MAXOUT ;  /* append END */
  306.             lcm = -1 ;
  307.          } else {
  308.             addts((unsigned char)(END)) ;
  309.          }
  310.       } /* end 'else rows different' */
  311. #ifdef DEBUG
  312.       if (d != specdata + width)
  313.          error("! internal inconsistency in repack") ;
  314. #endif
  315.    } /* end 'for j' */
  316.    if (repeatcount) {
  317.       while (repeatcount > MAXOUT) {
  318.          addts((unsigned char)(REPX+MAXOUT)) ;
  319.          repeatcount -= MAXOUT ;
  320.       }
  321.       addts((unsigned char)(REPX+repeatcount)) ;
  322.       repeatcount = 0 ;
  323.    }
  324. }
  325.  
  326. extern long bytesleft ;
  327. extern quarterword *raster ;
  328. long mbytesleft ;
  329. quarterword *mraster ;
  330.  
  331. char *
  332. makecopy(what, len, p)
  333. register unsigned char *what ;
  334. register long len ;
  335. register unsigned char *p ;
  336. {
  337.    register unsigned char *q ;
  338.  
  339.    if (p == NULL) {
  340.       if (len > MINCHUNK)
  341.          p = (unsigned char *)mymalloc((integer)len) ;
  342.       else {
  343.          if (bytesleft < len) {
  344.             raster = (quarterword *)mymalloc((integer)RASTERCHUNK) ;
  345.             bytesleft = RASTERCHUNK ;
  346.          }
  347.          p = (unsigned char *)raster ;
  348.          bytesleft -= len ;
  349.          raster += len ;
  350.       }
  351.    }
  352.    q = p ;
  353.    while (len > 0) {
  354.       *p++ = *what++ ;
  355.       len -- ;
  356.    }
  357.    return ((char *)q) ;
  358. }
  359.  
  360. /* Now the main routine, which is called when a character is used
  361.    for the very first time. */
  362. void repack(cp)
  363. register chardesctype *cp ;
  364. {
  365.    register long i, j ;
  366.    register unsigned char *p ;
  367.    register int width, height ;
  368.    register int wwidth ;
  369.    char startbytes ;
  370.    int smallchar ;
  371.  
  372.    p = cp->packptr ;
  373.    if (p == NULL)
  374.       error("! no raster?") ;
  375.    tsp = tempstore ;
  376.    tsend = tempstore + tslen ;
  377.    addts(*p) ; /* copy the PK flag byte */
  378.    if (*p & 4) {
  379.       if ((*p & 7) == 7) {
  380.          startbytes = 17 ;
  381.          width = getlong(p + 1) ;
  382.          height = getlong(p + 5) ;
  383.          for (i=0; i<12; i++)
  384.             addts(*++p) ;
  385.       } else {
  386.          startbytes = 9 ;
  387.          width = p[1] * 256 + p[2] ;
  388.          height = p[3] * 256 + p[4] ;
  389.          addts(*++p) ;
  390.          addts(*++p) ;
  391.          addts(*++p) ;
  392.          addts(*++p) ;
  393.       }
  394.    } else {
  395.       startbytes = 5 ;
  396.       width = p[1] ;
  397.       height = p[2] ;
  398.    }
  399.    addts(*++p) ;
  400.    addts(*++p) ;
  401.    addts(*++p) ;
  402.    addts(*++p) ;
  403.    p++ ; /* OK, p now points to beginning of the nibbles */
  404.    addts((unsigned char)0) ;
  405.    addts((unsigned char)0) ;
  406.    addts((unsigned char)0) ;
  407.    addts((unsigned char)0) ; /* leave room to stick in a new length field */
  408.    wwidth = (width + 15) / 16 ;
  409.    i = 2 * height * (long)wwidth ;
  410.    if (i <= 0)
  411.       i = 2 ;
  412.    if ((cp->flags & BIGCHAR) == 0)
  413.       smallchar = 5 ;
  414.    else
  415.       smallchar = 0 ;
  416.    i += smallchar ;
  417.    if (mbytesleft < i) {
  418.       if (mbytesleft >= RASTERCHUNK)
  419.          (void) free((char *) mraster) ;
  420.       if (RASTERCHUNK > i) {
  421.          mraster = (quarterword *)mymalloc((integer)RASTERCHUNK) ;
  422.          mbytesleft = RASTERCHUNK ;
  423.       } else {
  424.          i += i / 4 ;
  425.          mraster = (quarterword *)mymalloc((integer)(i + 3)) ;
  426.          mbytesleft = i ;
  427.       }
  428.    }
  429.    while (i > 0)
  430.       mraster[--i] = 0 ;
  431.    i = startbytes + unpack(p, (halfword *)mraster, (halfword)width,
  432.                 (halfword)height,  *(cp->packptr)) ;
  433.    dochar(mraster, (width + 7) >> 3, height) ;
  434.    if (smallchar) {
  435.       addts((unsigned char)0) ;
  436.       addts((unsigned char)0) ;
  437.       addts((unsigned char)0) ;
  438.       addts((unsigned char)0) ;
  439.       addts((unsigned char)0) ;
  440.    }
  441.    j = tsp - tempstore ;
  442. #ifdef DEBUG
  443.    if (dd(D_COMPRESS))
  444.         (void)fprintf(stderr,"PK %ld bytes, unpacked %ld, compressed %ld\n",
  445.        i-(long)startbytes, (long)((width+7L)/8)*height, j-(long)startbytes-4) ;
  446. #endif /* DEBUG */
  447.    if ( i < j ) {
  448.       if (i > MINCHUNK)
  449.          free(cp->packptr) ;
  450.       cp->packptr =
  451.               (unsigned char *)makecopy(tempstore, j, (unsigned char *)0) ;
  452.    } else
  453.       makecopy(tempstore, j, (unsigned char *)cp->packptr) ;
  454.    putlong((char *)(cp->packptr+startbytes), j-startbytes-4-smallchar) ;
  455.    cp->flags |= REPACKED ;
  456. }
  457.